package com.weitu.observation.statisticsAnalysis.service.impl;


import com.weitu.observation.statisticsAnalysis.entity.StatisticAnalysisType;
import com.weitu.observation.statisticsAnalysis.entity.StatisticsAnalysis;
import com.weitu.observation.statisticsAnalysis.mapper.StatisticAnalysisMapper;
import com.weitu.observation.statisticsAnalysis.service.StatisticAnalysisService;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/14.
 */
@Service
public class StatisticAnalysisServiceImpl implements StatisticAnalysisService {

    private final StatisticAnalysisMapper statisticAnalysisMapper;
    private final DepartmentMapper departmentMapper;

    @Autowired
    public StatisticAnalysisServiceImpl(StatisticAnalysisMapper statisticAnalysisMapper,DepartmentMapper departmentMapper){
        this.statisticAnalysisMapper=statisticAnalysisMapper;
        this.departmentMapper=departmentMapper;
    }

    @Override
    public List<Map<String,Object>> queryStatisticsAnalysisList(Map<String, Object> map) {
        List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();

        xunHuan("0",mapList,map);

        return mapList;
    }

    private void xunHuan(String parentId,List<Map<String,Object>> mapList,Map<String, Object> map){
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(parentId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                StatisticsAnalysis statistic=this.statisticAnalysisMapper.queryStatisticsAnalysis(map);
                StatisticsAnalysis statistics=combine(statistic,map);
                Map<String,Object> paramMap=objToMap(statistics);
                mapList.add(paramMap);
                xunHuan(id,mapList,map);
            }
    }
    private Map<String,Object> objToMap(StatisticsAnalysis allAnalysis){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        paramMap.put("id",allAnalysis.getId());
        paramMap.put("name",allAnalysis.getName());
        paramMap.put("parentId",allAnalysis.getParentId());
        paramMap.put("allCount",allAnalysis.getAllCount());
        paramMap.put("safeActCount",allAnalysis.getSafeActCount());
        paramMap.put("actCount",allAnalysis.getActCount());
        paramMap.put("stateCount",allAnalysis.getStateCount());
        return paramMap;
    }
    private void combine(StatisticsAnalysis one,StatisticsAnalysis two){
        one.setAllCount((one.getAllCount()==null?0:one.getAllCount())+(two.getAllCount()==null?0:two.getAllCount()));
        one.setActCount((one.getActCount()==null?0:one.getActCount())+(two.getActCount()==null?0:two.getActCount()));
        one.setStateCount((one.getStateCount()==null?0:one.getStateCount())+(two.getStateCount()==null?0:two.getStateCount()));
        one.setSafeActCount((one.getSafeActCount()==null?0:one.getSafeActCount())+(two.getSafeActCount()==null?0:two.getSafeActCount()));
    }
    private StatisticsAnalysis combine(StatisticsAnalysis one,Map<String,Object> map){
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(map.get("deptId").toString());
        if(parentDepartmentList.size()>0){
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                StatisticsAnalysis two=this.statisticAnalysisMapper.queryStatisticsAnalysis(map);
                combine(one,combine(two,map));
            }
        }
        return one;
    }


    @Override
    public  List<StatisticAnalysisType> queryStatisticsAnalysisByDictionaryType(Map<String, Object> map) {
        return combineByType(map);
    }

    private void combineByType(List<StatisticAnalysisType> one,List<StatisticAnalysisType> two){
        for(int i=0;i<one.size();i++){
            for(int j=0;j<two.size();j++){
                if(two.get(j).getId().equals(one.get(i).getId())){
                    one.get(i).setNum(one.get(i).getNum()+two.get(j).getNum());
                }
            }

        }
    }
    private List<StatisticAnalysisType> combineByType(Map<String,Object> map){
        List<StatisticAnalysisType> analysisTypeList=this.statisticAnalysisMapper.queryStatisticsAnalysisByDictionaryType(map);
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(map.get("deptId").toString());
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                combineByType(analysisTypeList,combineByType(map));
            }
        return analysisTypeList;
    }

    @Override
    public List<Map<String,Object>> queryBeanStatisticsAnalysis(Map<String, Object> map) {
       List<Map<String,Object>> MapList=new ArrayList<Map<String, Object>>();
        xunHuanBean("0",MapList,map);
        return MapList;
    }

    private void xunHuanBean(String parentId,List<Map<String,Object>> mapList,Map<String, Object> map){
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(parentId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                StatisticsAnalysis statistic=this.statisticAnalysisMapper.queryBeanStatisticsAnalysis(map);
                StatisticsAnalysis statistics=combineBean(statistic,map);
                Map<String,Object> paramMap=objToMap(statistics);
                mapList.add(paramMap);
                xunHuan(id,mapList,map);
            }
    }
    private StatisticsAnalysis combineBean(StatisticsAnalysis one,Map<String,Object> map){
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(map.get("deptId").toString());
        if(parentDepartmentList.size()>0){
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                map.put("deptId",id);
                StatisticsAnalysis two=this.statisticAnalysisMapper.queryBeanStatisticsAnalysis(map);
                combine(one,combineBean(two,map));
            }
        }
        return one;
    }

    @Override
    public List<StatisticsAnalysis> queryContractorStatisticsAnalysis(Map<String, Object> map) {
        return this.statisticAnalysisMapper.queryContractorStatisticsAnalysis(map);
    }

    @Override
    public int queryContractorCount(Map<String, Object> map) {
        return this.statisticAnalysisMapper.queryContractorCount(map);
    }
}
